using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Ink;
using System.Windows;
using System.Xml.Serialization;

using WozLib.UI.Ink;
using System.Diagnostics;
using WozLib.UI.Ink.SketchObjects;

namespace WozLib.UI.Ink.SketchCommands
{
  public class ModifyUserCurrentInkTool : RememberSketchCommand
  {

    [XmlElement("NewTool")]
    public SketchPad.InkTools NewTool
    {
      get { return _NewTool; }
      set { _NewTool = value; }
    } private SketchPad.InkTools _NewTool = SketchPad.InkTools.Selector;

    [XmlElement("PreviousTool")]
    public SketchPad.InkTools PreviousTool
    {
      get { return _PreviousTool; }
      set { _PreviousTool = value; }
    } private SketchPad.InkTools _PreviousTool = SketchPad.InkTools.Selector;

    [XmlElement("RememberCommand")]
    public bool RememberCommand
    {
      get { return _RememberCommand; }
      set { _RememberCommand = value; }
    } private bool _RememberCommand;

    public ModifyUserCurrentInkTool()
    {
    }

    public ModifyUserCurrentInkTool(SketchPad.InkTools newTool, SketchPad.InkTools previousTool)
      : base()
    {
      NewTool = newTool;
      PreviousTool = previousTool;
    }

    public ModifyUserCurrentInkTool(SketchPad.InkTools newTool)
      : base()
    {
      NewTool = newTool;
      // todo keep track that we need to save and load this ourself
      RememberCommand = true;
      RegisterObject();
    }

    public override void Redo()
    {
      try
      {
        if (this.WozClientCommandReferences.InkCanvas != null)
        {
          if (RememberCommand)
          {
            RegisterObject();
            PreviousTool = this.WozClientCommandReferences.InkCanvas.UserCurrentInkTool;
          }

          this.WozClientCommandReferences.InkCanvas.UserCurrentInkTool = NewTool;
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

    public override void Undo()
    {
      try
      {
        if (RememberCommand)
        {
          RememberSketchCommand wcrc = GetCommand(WozClientClearCommandID);
          if (wcrc != null && wcrc is ModifyUserCurrentInkTool)
          {
            ModifyUserCurrentInkTool modifyUserCurrentInkTool = wcrc as ModifyUserCurrentInkTool;
            if (modifyUserCurrentInkTool.WozClientCommandReferences != null)
            {
              if (modifyUserCurrentInkTool.WozClientCommandReferences.InkCanvas != null)
              {
                modifyUserCurrentInkTool.WozClientCommandReferences.InkCanvas.UserCurrentInkTool = modifyUserCurrentInkTool.PreviousTool;
              }
            }
          }
        }
        else
        {
          if (this.WozClientCommandReferences.InkCanvas != null)
          {
            this.WozClientCommandReferences.InkCanvas.UserCurrentInkTool = PreviousTool;
          }
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

  }
}

